#!groovy

pipeline {
	parameters {   
        choice(choices: 'us-west-2\nus-east-1', description: 'What AWS region?', name: 'aws_region')
		choice(choices: '287316796928', description: '287316796928 (lower env)', name: 'awsAccountId')
		choice(choices: '905404866763', description: '905404866763 (higher env)', name: 'awsProdAccountId')
    }
	environment {
		aws_region="${params.aws_region}"
	    dp_ECR_URI="${params.awsAccountId}.dkr.ecr.${params.aws_region}.amazonaws.com/goclouds-devops/configsever"
	    dp_ECR_URI_PROD="${params.awsProdAccountId}.dkr.ecr.${params.aws_region}.amazonaws.com/goclouds-devops/configsever"
 	}
	agent any
	stages {
		stage('Checkout Code'){
			steps {
				echo "download source code ..." 
                sh '''
                if [ -d "./microservice-config-server" ];then
                    rm -fr ./microservice-config-server
                fi
                git clone git@gitee.com:YanJun-ios/microservice-config-server.git
                '''
	        	echo "download source code successed" 
			}
		}
		stage('Preparation before build'){
			steps {

			sh '''
				echo "install tools begin ..."
				echo "install tools end ..."

			'''
			}
		}
		stage('Security Scan'){
			steps {
				echo "Security Scan begin ..."
				echo "Security Scang end ..."
			}
		}
		stage('Static Code Scan'){
			steps {
				echo "static code scan begin ..."
				echo "static code scan end ..."
			}
		}
        stage('Unit Test and Build') { 
		   steps {
		    sh '''
			#!/bin/bash -ilex
			echo "mvn build ..."
			cd ./microservice-config-server
			mvn install
			echo "docker build ..."
			docker build --tag configure:latest --file ./Dockerfile .
			'''
		   }
        }
        stage('push image') {
		    steps {
		        // Run the maven build
		        sh '''
				#!/bin/bash -ilex
		        aws ecr get-login --no-include-email --region ${aws_region} | bash
		        time=$(date +%Y%m%d%H%M%S)
				repositoryURI=${dp_ECR_URI}:${BUILD_NUMBER}_${time}
				docker tag configure:latest ${repositoryURI}
				docker push ${repositoryURI}
				
				echo "Writing image definitions file ..."
				printf '[{"name":"container-ctd-config-server","imageUri":"%s"}]' $repositoryURI > imagedefinitions-config-sever.json

				prodRepositoryURI=${dp_ECR_URI_PROD}:configure_${BUILD_NUMBER}_${time}
				printf '[{"name":"container-ctd-config-server","imageUri":"%s"}]' $prodRepositoryURI > imagedefinitions-config-sever-prod.json

cat > buildspec.yml << 'EOF'
version: 0.2
phases:
  build:
    commands:
      - 'aws --version'
      - 'aws sts get-caller-identity'
      - '$(aws ecr get-login --registry-ids awsAccountId --no-include-email --region aws_region)'
      - 'docker pull sourceImage'
      - 'docker images'
      - 'docker tag sourceImage targetImage'
      - '$(aws ecr get-login --registry-ids awsProdAccountId --no-include-email --region aws_region)'
      - 'docker push targetImage'
EOF

				sed -i '' "s!sourceImage!$repositoryURI!g" buildspec.yml
				sed -i '' "s!targetImage!$prodRepositoryURI!g" buildspec.yml
				sed -i '' "s!aws_region!$aws_region!g" buildspec.yml
				sed -i '' "s!awsAccountId!$awsAccountId!g" buildspec.yml
				sed -i '' "s!awsProdAccountId!$awsProdAccountId!g" buildspec.yml

				echo "Push definitions file to s3 bucket"
				zip -r imagedefinitions-config-sever-${time}.zip imagedefinitions-config-sever.json imagedefinitions-config-sever-prod.json buildspec.yml
				aws s3 cp ./imagedefinitions-config-sever-${time}.zip s3://mstar-pipiline-source-demo/imagedefinitions-config-sever.zip

				echo "finished"

				'''  
	     	}		
        }
	}
}